package com.orm.analysis.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.profiling.UtilTimerStack;
import com.orm.analysis.model.*;
import com.orm.analysis.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class CustomerAction extends FrontendAction {


    private GenericService<Customer> service;

    private GenericService<Address> addressService;

    private GenericService<Item> itemService;

    private GenericService<ContactInfo> contactInfoService;
    private Customer customer;
    private Address address;
    private Item item;
    private List<ContactInfo> contactInfoList;
    private List<Customer> customers;
    private List<Address> addresses;
    private List<Item> items;

    public CustomerAction( GenericService<Customer> service, GenericService<Address> addressService, GenericService<Item> itemService, GenericService<ContactInfo> contactInfoService) {

        this.service = service;
        this.addressService = addressService;
        this.itemService = itemService;
        this.contactInfoService = contactInfoService;
    }

    public String getAllCustomers() {
//        System.setProperty(UtilTimerStack.ACTIVATE_PROPERTY, "true");
//        String activityName = "getAllCustomers";
//        UtilTimerStack.setActive(true);
//        UtilTimerStack.push(activityName);
//        try {
//            customers =  service.list(page, size);
//        } finally {
//            UtilTimerStack.pop(activityName);
//
//        }
        customers = service.list(page, size);
//        customers = service.findAllWithDepth("contactInfo");


        return SUCCESS;
    }


    public String getBulkCustomers() {
           try {
               customers = service.findAllWithDepth("contactInfo");

           } catch (Exception e) {
               e.printStackTrace();
           }
           return SUCCESS;
       }


    public String getAllCustomerCustOrder() {


        customers = service.findAllWithDepth("custOrders");

        return SUCCESS;
    }


    public String getSingleCustomer() {


//        customer = service.findWithDepth("id",customer.getId(),"contactInfo");
        customer = service.findWithDepth("id",new Integer(46) ,"contactInfo");
        customers = new ArrayList<Customer>();
                customers.add(customer);
        return SUCCESS;
    }

    public String getCustomerAddressItem() {


//        customer = service.findWithDepth("id",customer.getId());
        customer = service.findWithDepth("id",new Integer(46) );
//        address = addressService.findWithDepth("AddressId",address.getAddressId());
        address = addressService.findWithDepth("addressId",new Integer(10));
//        item = itemService.findWithDepth("itemId",item.getItemId());
        item = itemService.findWithDepth("itemId",new Integer(1));
        customers = new ArrayList<Customer>();
        customers.add(customer);
        addresses = new ArrayList<Address>();
        addresses.add(address);
        items = new ArrayList<Item>();
        items.add(item);

        return SUCCESS;
    }


    public String getSingleCustomerTwice() {


//        customer = service.findWithDepth("id",customer.getId(),"contactInfo");
        customer = service.findWithDepth("id",new Integer(46),"contactInfo");
        customer = service.findWithDepth("id",new Integer(46),"contactInfo");
        customers = new ArrayList<Customer>();
        customers.add(customer);

        return SUCCESS;
    }

    public String getCRUD() {


        if (!validationSuccessful()) {
                    return "input";
                } else {
                    if (customer.getId() == null) {
                        customer =service.create(customer);
                        customer =service.getById(customer);
                        customer.setFirstName("Nimal");
                        customer =service.update(customer);
                        service.delete(customer.getId());
                    }

                }

        return SUCCESS;
    }

    public String list() {
        customers = service.list(page, size);
        return SUCCESS;
    }

    public String setUpForInsert() {
        prep();

        return "success";
    }
    public String setUpForUpdate() {
           prep();
           if (customer != null && customer.getId() != null) {
               customer = service.getById(customer.getId());
           }
           return "success";
       }


    public String insert() {



        if (!validationSuccessful()) {
            return "input";
        } else {
            if (customer.getId() == null) {
                service.create(customer);
            }

        }
        return "success";
    }

    public String update() {

        prep();

        if (!validationSuccessful()) {
            return "input";
        } else {

                service.update(customer);


        }
        return "success";
    }




    private void prep() {
        contactInfoList = contactInfoService.list(page, size);
        Map session = ActionContext.getContext().getSession();
        session.put("contactInfoList", contactInfoList);
    }




    public String delete() {
        service.delete(customer.getId());
        return SUCCESS;
    }


    private boolean validationSuccessful() {
        if (customer.getFirstName() == null || customer.getFirstName().trim().length() < 1) {
            addActionMessage("FirstName is required");
        }
        if (customer.getLastName() == null || customer.getLastName().trim().length() < 1) {
            addActionMessage("LastName is required");
        }

        if (this.hasActionMessages()) {
            return false;
        } else {
            return true;
        }


    }


    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }

    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }


    public GenericService<Customer> getService() {
        return service;
    }

    public void setService(GenericService<Customer> service) {
        this.service = service;
    }

    public GenericService<Address> getAddressService() {
        return addressService;
    }

    public void setAddressService(GenericService<Address> addressService) {
        this.addressService = addressService;
    }

    public GenericService<Item> getItemService() {
        return itemService;
    }

    public void setItemService(GenericService<Item> itemService) {
        this.itemService = itemService;
    }

    public GenericService<ContactInfo> getContactInfoService() {
        return contactInfoService;
    }

    public void setContactInfoService(GenericService<ContactInfo> contactInfoService) {
        this.contactInfoService = contactInfoService;
    }
}
